package com.beifu.manager.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.beifu.manager.dao.AssetTypeMapper;
import com.beifu.manager.dao.VenderMapper;
import com.beifu.manager.model.AssetIn;
import com.beifu.manager.model.AssetInExample;
import com.beifu.manager.model.AssetInExample.Criteria;
import com.beifu.manager.model.AssetInPlan;
import com.beifu.manager.model.AssetType;
import com.beifu.manager.model.AssetTypeExample;
import com.beifu.manager.model.Vender;
import com.beifu.manager.model.VenderExample;
import com.beifu.manager.service.assetin.AssetInService;
import com.beifu.manager.shiro.ActiveUser;
import com.mds.manager.utils.ResponseHelp;
import com.utils.PageUtils;

/**
 * 
* Title: AssetInController
* Description:资产入库（段）
* @author    BaiBing
* @date      2018年6月17日
 */
@Controller
public class AssetInController {
	
	private Logger Log = Logger.getLogger(this.getClass());
	
	@Autowired
	private AssetInService assetInService;
	@Autowired
	private AssetTypeMapper assetTypeMapper;
	@Autowired
	private VenderMapper venderMapper;
	/*@Autowired
	private AssetInPlanMapper assetInPlanMapper;*/
	
	@RequestMapping("/assetin/goAssetInIndex")
	public String goOrganizationIndex(Model model, HttpServletRequest request){
		Log.info(">> 资产入库列表界面");
		/*
		List<Tree> list = organizationService.selectTreeNodes("0");
		JSONArray json = JSONArray.fromObject(list);
		String rs = json.toString();
		rs = rs.replace("\"", "'");
		Log.info(rs);
		model.addAttribute("options", rs);*/
		
		return "/assetin/assetin_index";
	}
	
	
	/**
	 * 资产入库列表数据
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/assetin/getAssetInPage",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
	public String getPageData(Model model,HttpServletRequest request, HttpServletResponse res){
		String page = request.getParameter("pageNumber");
		String query_assetid = request.getParameter("query_assetid");//备品单编号
		String query_assetstatus = request.getParameter("query_assetstatus");//备品单状态
		
		AssetInExample example = new AssetInExample();
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(query_assetid)){
			criteria.andIdEqualTo(Long.valueOf(query_assetid));
		}
		if(!StringUtils.isEmpty(query_assetstatus)){
			criteria.andStatusEqualTo(Integer.valueOf(query_assetstatus));
		}
		
		PageUtils Page = new PageUtils(page);
		Page.setQueryParames(example);
		Page = assetInService.getAssetInPage(Page);
		return ResponseHelp.responseText(Page);
	}
	
	
	
	
	@RequestMapping("/assetin/goAssetInAdd")
	public String goAssetInAdd(Model model, HttpServletRequest request){
		Log.info(">> 添加备品入库单列表界面");
		
		//查询当前所有分类
		AssetTypeExample assetTypeExample = new AssetTypeExample();
		assetTypeExample.createCriteria();
		List<AssetType> assetTypeAll = assetTypeMapper.selectByExample(assetTypeExample);
		
		//查询当前所有供应商
		VenderExample venderExample = new VenderExample();
		venderExample.createCriteria();
		List<Vender> venderList = venderMapper.selectByExample(venderExample);
		
		Subject subject = SecurityUtils.getSubject();
		ActiveUser activeUser = (ActiveUser)subject.getPrincipal();
		String userName = activeUser.getUserName();
		
		request.setAttribute("assetTypeAll", assetTypeAll);
		request.setAttribute("userName", userName);
		request.setAttribute("venderList", venderList);
		
		return "/assetin/assetin_add";
	}
	
	
	
	@RequestMapping("/assetin/goAssetInView")
	public String goAssetInView(Model model, HttpServletRequest request){
		Log.info(">> 查看备品入库单详情界面");
		String rowJson = request.getParameter("rowJson");
		//String assetInId = request.getParameter("assetInId");
		
		//根据入库单编号，查询入库单类目数据
		
		//根据入库单编号，查询入库单列表
		/*JSONObject obj = new JSONObject().fromObject(rowJson);
		AssetIn assetIn = (AssetIn)JSONObject.toBean(obj,AssetIn.class);
		
		String assetInPlans = obj.getString("assetInPlans");
		JSONArray assetInPlansArray = JSONArray.fromObject(assetInPlans);
        List<?> assetInPlansList = JSONArray.toList(assetInPlansArray, new AssetInPlan(), new JsonConfig());
		
        assetIn.setAssetInPlans(assetInPlansArray);*/
		request.setAttribute("parent_row", rowJson);
        
		return "/assetin/assetin_view";
	}
	
	
	@ResponseBody
	@RequestMapping("/assetin/assetInAdd")
	public String assetInAdd(Model model, HttpServletRequest request){
		Log.info(">> 添加备品入库单");
		
		Subject subject = SecurityUtils.getSubject();
		ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
		
		
		String create_userName = request.getParameter("create_userName");
		String create_inTime = request.getParameter("create_inTime");
		String create_remark = request.getParameter("create_remark");
		
		String assetTypeSelect = request.getParameter("assetTypeSelect");
		String assetModelSelect  = request.getParameter("assetModelSelect");
		String assetTypeNum = request.getParameter("assetTypeNum");
		String vender = request.getParameter("vender");
		
		
		List<String>  assetTypeSelectArr = new ArrayList<String> ();
		List<String> assetModelSelectArr = new ArrayList<String> ();
		List<String>  assetTypeNumArr = new ArrayList<String> (); 
		
		
		if(!StringUtils.isEmpty(assetTypeSelect) && assetTypeSelect.indexOf(",") > 0){
			assetTypeSelectArr = (List<String>) Arrays.asList(assetTypeSelect.split(","));
		}else{
			assetTypeSelectArr.add(assetTypeSelect);
		}
		
		if(!StringUtils.isEmpty(assetModelSelect) && assetModelSelect.indexOf(",") > 0){
			assetModelSelectArr = (List<String>) Arrays.asList(assetModelSelect.split(","));
		}else{
			assetModelSelectArr.add(assetModelSelect);
		}
		
		if(!StringUtils.isEmpty(assetTypeNum) && assetTypeNum.indexOf(",") > 0){
			assetTypeNumArr = (List<String>) Arrays.asList(assetTypeNum.split(","));
		}else{
			assetTypeNumArr.add(assetTypeNum);
		}
		
		String status = "0";
		assetInService.createAsstInOrder(activeUser.getUserId(),activeUser.getOrganizeId(),
											create_inTime,status,assetTypeSelectArr,assetModelSelectArr,assetTypeNumArr,create_remark,vender);
		
		return ResponseHelp.responseText();
	}
	
	
	/**
	 * 删除备品入库单
	 * @param model
	 * @param request
	 * @param
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/assetin/delAssetIn")
	public String delAssetIn(Model model,HttpServletRequest request){
		String ids = request.getParameter("ids");
		String[] idsArr = ids.split(",");
		
		assetInService.batchDelAssetIn(idsArr);
		return ResponseHelp.responseText();
	}
	
	public static void main(String[] args) {
		String str = "{\"assetInPlans\":[{\"assetInId\":19,\"assetTypeId\":1,\"id\":4,\"inCount\":111,\"remark1\":\"\",\"remark2\":\"\",\"remark3\":\"\"},{\"assetInId\":19,\"assetTypeId\":2,\"id\":5,\"inCount\":222,\"remark1\":\"\",\"remark2\":\"\",\"remark3\":\"\"},{\"assetInId\":19,\"assetTypeId\":3,\"id\":6,\"inCount\":333,\"remark1\":\"\",\"remark2\":\"\",\"remark3\":\"\"},{\"assetInId\":19,\"assetTypeId\":1,\"id\":7,\"inCount\":444,\"remark1\":\"\",\"remark2\":\"\",\"remark3\":\"\"}],\"createTime\":{\"date\":18,\"day\":1,\"hours\":9,\"minutes\":49,\"month\":5,\"seconds\":2,\"time\":1529286542000,\"timezoneOffset\":-480,\"year\":118},\"createUserId\":1,\"createUserInfo\":{\"id\":1,\"loginName\":\"admin\",\"loginPassword\":\"a66abb5684c45962d887564f08346e8d\",\"organizeId\":2,\"roleId\":1,\"userName\":\"超级管理员\"},\"destOrganizeId\":2,\"id\":19,\"inTime\":{\"date\":28,\"day\":1,\"hours\":0,\"minutes\":0,\"month\":4,\"seconds\":0,\"time\":1527436800000,\"timezoneOffset\":-480,\"year\":118},\"orderImage\":\"\",\"remark1\":\"123123\",\"remark2\":\"\",\"remark3\":\"\",\"srcOrganizeId\":1,\"status\":0}";
		JSONObject obj = new JSONObject().fromObject(str);
		
		String assetInPlans = obj.getString("assetInPlans");
		
	
		AssetIn jb = (AssetIn)JSONObject.toBean(obj,AssetIn.class);
		System.out.println(jb);
		System.out.println(jb.getCreateUserInfo().getUserName());
		
		JSONArray array = JSONArray.fromObject(assetInPlans);
		// 转换方法2
        List<?> list2 = JSONArray.toList(array, new AssetInPlan(), new JsonConfig());//参数1为要转换的JSONArray数据，参数2为要转换的目标数据，即List盛装的数据
        System.out.println(((AssetInPlan)list2.get(0)).getInCount());
	}
}
